package com.emep.changzhi.analyse.http.basics;

import com.emep.changzhi.analyse.entity.basics.SysKeyTypeInfo;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.basics.SysKeyTypeService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author xy
 * @Description: 重点类型控制类
 * @date 2018/2/26 10:53
 * @version V1.0
 */
@RestController
@RequestMapping("key")
@Api(value="重点类型controller",tags={"重点类型操作接口"})
public class SysKeyTypeController {
    @Autowired
    private SysKeyTypeService keyTypeService;
    /**
     * 新增重点类型
     * @param payload
     * @return
     */
    @ApiOperation(value="新增重点类型", notes="新增重点类型")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "修改时传的id", required = false, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "keyTypeName", value = "重点类型名字", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "keyTypeCode", value = "系统层级码", required = true, dataType = "String",paramType="query"),
    })
    @PostMapping(value = "add")
    public ResultBean<SysKeyTypeInfo> add(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "keyTypeName","keyTypeCode");
        if(result.getCode() == 1){return  result.boolAdd(keyTypeService.save(payload)) ;}
        return  result;
    }

    /**
     * 根据id 删除重点类型
     * @param payload
     */
    @ApiOperation(value="删除重点类型", notes="根据id删除重点类型")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "重点类型id", required = true, dataType = "String",paramType="query"),
    })
    @DeleteMapping(value = "/delete")
    public ResultBean<Boolean> delete(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == 1){return result.bool(keyTypeService.deleteKeyType(payload.get("id").toString()));}
        return result;
    }

    /**
     * 查询所有重点类型
     * @return
     */
    @ApiOperation(value="查询所有重点类型", notes="无参数获取所有数据")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @GetMapping(value = "list")
    public ResultBean<List<SysKeyTypeInfo>> queryKeyType(){
        return new ResultBean<List<SysKeyTypeInfo>>(keyTypeService.findAllKeyTypes());
    }

    /**
     * 通过id 根据id查询重点类型
     * @param payload
     * @return
     */
    @ApiOperation(value="根据id查询重点类型", notes="根据id查询重点类型")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "重点类型id", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping(value = "info")
    public ResultBean<SysKeyTypeInfo> findKeyTypeById(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == 1) {
            return result.ok(keyTypeService.findKeyTypeById(payload.get("id").toString()));
        }
        return result;
    }
    /**
     * 分页查询重点类型
     * page，第几页，从0开始，默认为第0页
     * size，每一页的大小，默认为20
     * sort，排序相关的信息，以property,property(,ASC|DESC)的方式组织，例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列
     * @param
     * @return
     */
    @ApiOperation(value="分页查询重点类型", notes="分页无参数查询重点类型")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping("paging")
    public ResultBean<Page<SysKeyTypeInfo>> showKeyTypes(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            return result.ok(keyTypeService.findAll(Integer.parseInt(payload.get("pageIndex")
                    .toString()),Integer.parseInt(payload.get("pageSize").toString())));
        }
        return result;
    }

    /**
     * 分页查询重点类型
     * page，第几页，从0开始，默认为第0页
     * size，每一页的大小，默认为20
     * sort，排序相关的信息，以property,property(,ASC|DESC)的方式组织，例如sort=firstname&sort=lastname,desc表示在按firstname正序排列基础上按lastname倒序排列
     * @param
     * @return
     */
    @ApiOperation(value="分页带参数查询重点类型", notes="分页带参数查询重点类型")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "keyTypeName", value = "重点类型名称", required = false, dataType = "String",paramType="query")
    })
    @GetMapping("paging/query")
    public ResultBean<Page<SysKeyTypeInfo>> PageKeyTypes(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            SysKeyTypeInfo keyTypeInfo = new SysKeyTypeInfo();
            keyTypeInfo.setKeyTypeName(payload.containsKey("keyTypeName") ?payload.get("keyTypeName") != null ? payload.get("keyTypeName").toString():"":"" );
            Page<SysKeyTypeInfo> pageKeyTypeInfo = keyTypeService.findBookCriteria(Integer.parseInt(payload.get("pageIndex") .toString()),Integer.parseInt(payload.get("pageSize").toString()),keyTypeInfo);
            return result.ok(pageKeyTypeInfo);
        }
        return result;
    }
}
